package benchmarks;

//TODO nu deserializeren we dmv read/write-ClassAndObject --> zoek een manier om de andere soorten ook eens te testen!
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;

import edu.vub.at.objects.natives.NATText;
import edu.vub.at.objects.natives.grammar.AGAssignField;
import edu.vub.at.objects.natives.grammar.AGSymbol;
import edu.vub.at.parser.NATParser;

public class KryoAllKnowingLib implements TestMethods {
	
	public static Kryo kryo;

	KryoAllKnowingLib() {
		kryo = new Kryo();
		kryo.register(NATText.class);
		kryo.register(AGSymbol.class);
		kryo.register(AGAssignField.class);
		kryo.register(NATParser.class);
	}

	@Override
	public byte[] serialize(Object o) throws IOException {
		ByteArrayOutputStream buffer = new ByteArrayOutputStream();
		Output out = new Output(buffer);
		kryo.writeObject(out, o);
		out.flush();
		out.close();
		return buffer.toByteArray();
	}

	@Override
	public Object deserialize(byte[] b, Class c) throws IOException,
			ClassNotFoundException {
		ByteArrayInputStream in = new ByteArrayInputStream(b);
		Input input = new Input(in);
		return kryo.readObject(input, c); //TODO eigenlijk moet er nu nog uit het java.lang.Object object het juiste object gemaakt worden --> dit kan vertragen denk ik --> TESTEN!
	}

	@Override
	public Object copy(Object o) throws IOException, ClassNotFoundException {
		return kryo.copy(o);
	}

}
